package com.app.couponsharelife.ui.view

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

/**
 *Created by xct on 2019/1/15
 *Description:
 */
class MyRecyclerview:RecyclerView,Runnable{
    private var mIsRolling = false
    private var isTouchEvent=true
    private var scrollSpeed=1
    private var isTop=true
    private var isBottom=false
    constructor(context: Context):this(context,null)
    constructor(context: Context,attrs: AttributeSet?):this(context,attrs,0)
    constructor(context: Context,attrs: AttributeSet?,defStyle:Int):super (context,attrs,defStyle)
    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        startRoll()
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        stopRoll()
    }
    /**
     * 开始滑动
     */
    fun startRoll() {
        //已经滑动时/没有设置适配器时直接返回
        if (mIsRolling && adapter == null) return
        mIsRolling = true
        postDelayed(this,30)
    }

    /**
     * 停止滑动
     */
    fun stopRoll() {
        if(!mIsRolling)  return
        mIsRolling=false
        removeCallbacks(this)
    }
    fun setType(isTops:Boolean){
        if (isTops){
            isTop=true
            isBottom=false
        }else{
            isBottom=true
            isTop=false
        }
    }
    /**
     * 事件触摸开关
     */
    fun setTouch(isTouch:Boolean){
        isTouchEvent=isTouch
    }
    /**
     * 滑动速度
     */
    fun setScrollSpeed(speed:Int){
        scrollSpeed = if (speed>5){
            3
        }else{
            speed
        }
    }
    override fun onTouchEvent(e: MotionEvent?): Boolean {
        return if (isTouchEvent){
            when(e?.action){
                MotionEvent.ACTION_DOWN,MotionEvent.ACTION_MOVE -> stopRoll()
                MotionEvent.ACTION_UP,MotionEvent.ACTION_CANCEL,MotionEvent.ACTION_OUTSIDE -> startRoll()
            }
            super.onTouchEvent(e)
        }else{
            false
        }
        //1.若让滑动不受用户触摸影响，直接返回false，表示不处理事件
        //return false
        //2.若需要在用户触摸时停止，用户离开时开始，只需要根据情况触摸事件进行处理即可
    }
    override fun run() {
        if(this.mIsRolling){
            if (layoutManager is LinearLayoutManager){
                if ((layoutManager as LinearLayoutManager).orientation== LinearLayoutManager.VERTICAL){
                    scrollBy(0,scrollSpeed)
                    postDelayed(this,30)
                }else{
                    if (isTop&&!isBottom){
                        scrollBy(scrollSpeed,0)
                    }else{
                        scrollBy(-scrollSpeed,0)
                    }
                    postDelayed(this,30)
                }
            }else{
                if (isTop&&!isBottom){
                    scrollBy(scrollSpeed,0)
                }else{
                    scrollBy(-scrollSpeed,0)
                }
                postDelayed(this,30)
            }
        }
    }

}